package com.front.managementfront.driver.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.front.managementfront.driver.entity.HktsDriver;
import com.front.managementfront.driver.service.IHktsDriverService;
import com.front.managementfront.driver.vo.DriverParameterVo;
import com.front.managementfront.utils.CustomPageUtils;


import com.hkts.management.common.utils.CustomPage;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.vo.UserAndRealNameVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hml
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/front/hktsDriver")
@Api(tags = "司机用户管理")
public class HktsDriverController {
    private final Logger logger = LoggerFactory.getLogger(HktsDriverController.class);

    @Autowired
    public IHktsDriverService iHktsDriverService;


    /**
     * 分页查询数据
     *
     * @param page       分页信息
     * @param hktsDriver 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHktsDriverPageList")
    public Result<CustomPage<List<HktsDriver>>> getHontUserList(Page<HktsDriver> page, HktsDriver hktsDriver) {

        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsDriverService.selectPage(page, hktsDriver)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hktsDriver 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsDriverSave")
    public Result<Object> hktsDriverSave(HktsDriver hktsDriver) {
        iHktsDriverService.saveOrUpdate(hktsDriver);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsDriverGetById")
    public Result<Object> hktsDriverGetById(String id) {
        HktsDriver byId = iHktsDriverService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsDriverDelete")
    public Result<Object> hontUserDelete(String id) {
        iHktsDriverService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsDriverBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHktsDriverService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    /*
    * 根据司机id获取用户信息
    * */
    @ResponseBody
    @RequestMapping(method=RequestMethod.GET,value = "/getDriverInfoByUserId/{userId}")
    public Result<UserAndRealNameVo> getDriverInfoByUserId(@ApiParam(value = "司机id") @PathVariable String userId){
        UserAndRealNameVo vo=iHktsDriverService.getDriverInfoByUserId(userId);
        return ResultUtil.data(vo);
    }
    /*
    * 根据司机id获取用户信息
    * */
    @ResponseBody
    @RequestMapping(method=RequestMethod.GET,value = "/getDriverInfosByUserIds")
    public Result<Map<String, Object>> getDriverInfosByUserIds(@RequestParam("userIds") List<String> userIds){
        List<UserAndRealNameVo> vo=iHktsDriverService.getDriverInfosByUserIds(userIds);
        Map<String, Object> map=new ConcurrentHashMap<>();
        for (UserAndRealNameVo item:vo
        ) {
            map.put(item.getId(),item);
        }
        return ResultUtil.data(map);
    }
    
    /**
     * 修改司机认证状态
     * 认证状态修改后给司机端发送消息
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateDriverApproval")
    @ApiOperation(value = "司机认证接口")
    public Result<Object> updateDriverApproval(DriverParameterVo vo) {

        iHktsDriverService.updateDriverApproval(vo);

        return ResultUtil.success("修改成功");
    }
    /**
     * 拉黑/激活司机状态
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateDriverStatus")
    public Result<Object> updateDriverStatus(DriverParameterVo vo) {
        iHktsDriverService.updateDriverStatus(vo);
        return ResultUtil.success("修改成功");
    }
    /**
     * 根据姓名或手机号获取用户
     */
    @ResponseBody
    @RequestMapping(method=RequestMethod.GET,value = "/getDriverByUserNameOrMobile")
    public Result<HktsDriver> getDriverByUserNameOrMobile(@ApiParam(value = "司机姓名或手机号") String userName){
        HktsDriver driverByUserNameOrMobile = iHktsDriverService.getDriverByUserNameOrMobile(userName);
        return ResultUtil.data(driverByUserNameOrMobile);
    }
    /**
     * 根据手机号获取用户
     */
    @ResponseBody
    @RequestMapping(method=RequestMethod.GET,value = "/getDriverByMobile")
    public Result<Object> getDriverByMobile(@ApiParam(value = "手机号") CustomPage page, String mobile){
        if(mobile.length() <=9){
            List<Object> list = new ArrayList<>();
            return ResultUtil.data(list);
        }
        CustomPage driverInfoByMobile = iHktsDriverService.getDriverInfoByMobile(page, mobile);
        return ResultUtil.data(driverInfoByMobile);
    }
}
